package Service;

import Dao.Impl.AdminDAOImpl;
import Dao.Impl.CustomerDAOImpl;
import MyUtil.JDBCUtils;

public class LoginService {
    // 注入Dao
    private AdminDAOImpl adminDAOImpl;
    private CustomerDAOImpl customerDAOImpl;

    // 构造器
    public LoginService() throws Exception {
        // 创建Dao对象
        adminDAOImpl = new AdminDAOImpl();
        customerDAOImpl = new CustomerDAOImpl();
    }

    // 管理员登录
    public int serviceAdministratorsLogin(String username, String password) throws Exception {
        // 调用DAO层验证管理员用户名和密码是否正确
        int isValidCredentials = adminDAOImpl.administratorLogin(username, password);
        return isValidCredentials;
    }

    // 管理员注册
    public boolean serviceAdministratorsRegister(String username, String password) throws Exception {
        // 调用DAO层验证管理员用户名是否已存在
        boolean isUsernameExist = adminDAOImpl.checkAdminNameExist(username);
        if (isUsernameExist) {
            return false; // 用户名已存在，注册失败
        }
        // 调用DAO层将管理员信息插入数据库
        boolean isRegisterSuccess = adminDAOImpl.administratorRegister(username, password);
        return isRegisterSuccess;
    }

    // 用户登录
    public int serviceUserLogin(String username, String password) throws Exception {
        // 调用DAO层验证用户的用户名和密码是否正确
        int isValidCredentials = customerDAOImpl.userLogin(username, password);
        return isValidCredentials;
    }

    // 用户注册
    public boolean serviceUserRegister(String username, String password,String registrationDate) throws Exception {
        // 调用DAO层验证用户名是否已存在
        boolean isUsernameExist = customerDAOImpl.checkUserNameExist(username);
        if (isUsernameExist) {
            return false; // 用户名已存在，注册失败
        }
        // 调用DAO层将用户信息插入数据库
        boolean isRegisterSuccess = customerDAOImpl.userRegister(username, password,registrationDate);
        return isRegisterSuccess;
    }
}
